package space.jevin.jevinboot.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import space.jevin.jevinboot.constant.SexEnum;
import space.jevin.jevinboot.entity.BaseUserDetailParam;
import space.jevin.jevinboot.exception.BaseException;
import space.jevin.jevinboot.exception.business.AccessErrorCode;
import space.jevin.jevinboot.exception.system.ErrorCode;
import space.jevin.jevinboot.exception.unknown.UnknownErrorCode;
import space.jevin.jevinboot.utils.validate.ValidateGroup;

import javax.validation.constraints.NotBlank;

/**
 * Demo请求层
 *
 * @author zhangjiehang
 * @version V1.0
 * @date 2021/5/11 11:11 上午
 */
@RestController
@Validated
@Slf4j
public class DemoController {

    @GetMapping("/hello")
    public String hello(@NotBlank(message = "context is required") String context){
        return context;
    }

    @PostMapping("/hello2")
    public BaseUserDetailParam hello2(@RequestBody @Validated({ValidateGroup.Add.class}) BaseUserDetailParam baseUserDetailParam) throws Exception{
        SexEnum sexEnum = SexEnum.valueOf(baseUserDetailParam.getSex());
        // 打印枚举值
        log.info(sexEnum.name());
        // 测试抛异常
        if (sexEnum.equals(SexEnum.FEMALE)){
            throw new BaseException(AccessErrorCode.A0300);
        }
        if ("system".equals(baseUserDetailParam.getNickname())){
            throw new BaseException(ErrorCode.SUCCESS);
        }
        if ("unknown".equals(baseUserDetailParam.getNickname())){
            throw new BaseException(UnknownErrorCode.UNKNOWN);
        }
        return baseUserDetailParam;
    }
}
